package com.example.chatplatform.mapper;

import com.example.chatplatform.model.entity.User;
import com.example.chatplatform.model.vo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("select user_id from user where user_account = #{userAccount}")
    Integer user(User user);

    @Insert("insert into user(user_name, user_avatar, user_account, user_password, user_time) " +
            "values (#{user.userName}, #{FileUrl}, #{user.userAccount}, #{user.userPassword}, #{user.userTime})")
    Integer userRegister(User user, String FileUrl);

    @Select("select user_id, user_account from user where user_account = #{userAccount} and user_password = #{userPassword}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userAccount", column = "user_account")
    })
    User userLogin(String userAccount, String userPassword);

    @Select("select user_id, user_name, user_avatar, user_account, user_password, user_time, user_gender, user_age, user_address from user where user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userAvatar", column = "user_avatar"),
            @Result(property = "userAccount", column = "user_account"),
            @Result(property = "userPassword", column = "user_password"),
            @Result(property = "userTime", column = "user_time"),
            @Result(property = "userGender", column = "user_gender"),
            @Result(property = "userAge", column = "user_age"),
            @Result(property = "userAddress", column = "user_address")
    })
    User userInfo(Integer userId);

    @Update("update user set user_name = #{userName}, user_gender = #{userGender}, user_age = #{userAge}, user_address = #{userAddress} where user_id = #{userId}")
    int alterUser(User user);

    @Select("select user_avatar from user where user_id = #{userId}")
    String avatar(Integer userId);

    @Update("update user set user_name = #{user.userName}, user_avatar = #{fileUrl}, user_gender = #{user.userGender}, user_age = #{user.userAge}, user_address = #{user.userAddress} where user_id = #{user.userId}")
    int alterUserWithAvatar(User user, String fileUrl);

    @Select("select user_password from user where user_id = #{userId}")
    String password(int userId);

    @Update("update user set user_password = #{newPassword} where user_id = #{userId}")
    void alterPassword(int userId, String newPassword);

    @Delete("delete from user_crony where user_id = #{userId} or crony_id = #{userId}")
    void deleteUserCrony(Integer userId);

    @Delete("delete from crony_message where messaging_id = #{userId} or receive_id = #{userId}")
    void deleteUserCronyMessage(Integer userId);

    @Select("select group_id from group_user where user_id = #{userID}")
    List<Integer> userGroup(Integer userId);

    @Delete("delete from group_user where group_id = #{item} and user_id = #{userId}")
    void deleteGroupUser(Integer userId, Integer item);

    @Select("select COUNT(group_user_id) from group_user where group_id = #{item}")
    Integer groupNumber(Integer item);

    @Update("update `group` set group_number = #{i} where group_id = #{item}")
    void updateGroupNumber(Integer item, int i);

    @Update("update user set " +
            "user_name = #{deleteUserName}, " +
            "user_avatar = #{deleteUserAvatar}, " +
            "user_account = null, " +
            "user_password = null, " +
            "user_gender = null, " +
            "user_age = null, " +
            "user_address = null " +
            "where user_id = #{userId}")
    int deleteUser(Integer userId,String deleteUserName, String deleteUserAvatar);

    @Insert("insert into delete_user(user_id) " +
            "values (#{userId})")
    Integer addDeleteUser(Integer userId);

    @Select("select user.user_id, user_name, user_avatar from user_crony " +
            "join user on user.user_id = user_crony.crony_id" +
            " where user_crony.user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userAvatar", column = "user_avatar")
    })
    List<CronyInfoVO> cronyInfo(Integer userId);

    @Select("select `group`.group_id, group_name, group_avatar from group_user " +
            "join `group` on `group`.group_id = group_user.group_id" +
            " where group_user.user_id = #{userId}")
    @Results({
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "groupName", column = "group_name"),
            @Result(property = "groupAvatar", column = "group_avatar")
    })
    List<GroupInfoVO> groupInfo(Integer userId);

    @Select("select user_id, user_name, user_account, user_avatar from user " +
            "where " +
            "user_name LIKE CONCAT('%', #{searchObjectValue}, '%') or " +
            "user_account LIKE CONCAT('%', #{searchObjectValue}, '%')")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userAccount", column = "user_account"),
            @Result(property = "userAvatar", column = "user_avatar")
    })
    List<SearchUserVO> searchCronyInfo(String searchObjectValue);

    @Select("select group_id, group_name, group_account, group_avatar from `group` " +
            "where " +
            "group_name LIKE CONCAT('%', #{searchObjectValue}, '%') or " +
            "group_account LIKE CONCAT('%', #{searchObjectValue}, '%')")
    @Results({
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "groupName", column = "group_name"),
            @Result(property = "groupAccount", column = "group_account"),
            @Result(property = "groupAvatar", column = "group_avatar")
    })
    List<SearchGroupVO> searchGroupInfo(String searchObjectValue);

    @Select("select user_id from delete_user")
    List<Integer> deleteUserId();

    @Select("select group_id from delete_group")
    List<Integer> deleteGroupId();

    @Select("select user_id, user_name, user_avatar, user_account, user_gender, user_age, user_address from user where user_id = #{cronyId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userAvatar", column = "user_avatar"),
            @Result(property = "userAccount", column = "user_account"),
            @Result(property = "userGender", column = "user_gender"),
            @Result(property = "userAge", column = "user_age"),
            @Result(property = "userAddress", column = "user_address")
    })
    UserDetailVO getUserInfo(Integer cronyId);

    @Select("select user_crony_id from user_crony where user_id = #{userId} and crony_id = #{cronyId}")
    Integer ifUserCrony(Integer userId, Integer cronyId);

}
